package com.atguigu.d_test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo1 {
    public static void main(String[] args) {
        //准备牌
        String[] flowers = {"♠","♥","♣","♦"};
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

        //准备牌盒
        ArrayList<String> box = new ArrayList<>();

        for (String flower : flowers) {
            for (String number : numbers) {
                String poke = flower + number;
                //添加到box去
                box.add(poke);
            }
        }

        //差大小王
        box.add("joker");
        box.add("JOKER");

        //System.out.println("box = " + box);

        //洗牌
        //Collections.shuffle(box,new Random(48));//种子
        Collections.shuffle(box);//
        //System.out.println("box = " + box);

        //准备牌搭子
        ArrayList<String> 地主牌 = new ArrayList<>();
        ArrayList<String> 周润发 = new ArrayList<>();
        ArrayList<String> 刘德华 = new ArrayList<>();
        ArrayList<String> 周星驰 = new ArrayList<>();
        //发牌
        //最后三张地主牌
        地主牌.add(box.get(box.size() - 1));
        地主牌.add(box.get(box.size() - 2));
        地主牌.add(box.get(box.size() - 3));

        //均分逻辑 :  %  均分几份就%几  x % 3
        for (int i = 0; i < box.size() - 3; i++) {
            //判断i
            if (i % 3 == 0){
                周润发.add(box.get(i));
            }else if (i % 3 == 1){
                刘德华.add(box.get(i));
            }else {
                周星驰.add(box.get(i));
            }
        }

        //准备带索引的容器
        ArrayList<String> list = new ArrayList<>();
        //批量添加元素
        Collections.addAll(list,"3","4","5","6","7","8","9","10","J","Q","K","A","2","oker","OKER");
        //独立比较器
        Comparator<String> rule = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //去除牌的花色
                String s1 = o1.substring(1);//♣3 -> 3
                String s2 = o2.substring(1);//♥2 -> 2
                //找截取后的牌在集合中的索引位置
                int i1 = list.indexOf(s1);//3 -> 0索引
                int i2 = list.indexOf(s2);//2 -> 12索引
                return i1 - i2;
            }
        };
        //理牌 [排序] -> 移花接木
        Collections.sort(地主牌,rule);
        Collections.sort(周润发,rule);
        Collections.sort(刘德华,rule);
        Collections.sort(周星驰,rule);

        //看牌
        System.out.println("地主牌 = " + 地主牌);
        System.out.println("周润发 = " + 周润发);
        System.out.println("刘德华 = " + 刘德华);
        System.out.println("周星驰 = " + 周星驰);
    }
}
